package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;
    @Override
    public void add(ProductBean product) {
          productMapper.insert(product);
    }

    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum, String salePrice, String type) {
        QueryWrapper<ProductBean>qw=new QueryWrapper<>();
        if (name!=null){
            qw.like("p_name",name);
        }
        if (startNum!=null){
            qw.ge("p_num",startNum);
        }
        if (endNum!=null){
            qw.le("p_num",endNum);
        }
        if ("未定销售价".equals(salePrice)){
            qw.isNull("p_salePrice");
        }
        if ("已定销售价".equals(salePrice)){
            qw.isNotNull("p_salePrice");
        }
        if (type!=null){
            qw.like("p_type",type);
        }
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ProductBean findById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updateSalePrice(Integer id, Integer newSalePrice) {
        ProductBean productBean=productMapper.selectById(id);
        productBean.setSalePrice(newSalePrice);
        productMapper.updateById(productBean);
    }

    @Override
    public void updateNum(Integer productId, Integer Price, Integer num, String isInKu) {
        ProductBean product=productMapper.selectById(productId);
        if ("isInKu".equals(isInKu)){//入库
            product.setBuyPrice(Price);
            product.setNum(product.getNum()+num);
            productMapper.updateById(product);
        }else {//出库
            product.setNum(product.getNum()-num);
            productMapper.updateById(product);
        }
    }

}
